<template>
  <main class="post">
    <a-form-model ref="roleForm"
                  :rules="rules"
                  :label-col="labelCol"
                  :wrapper-col="wrapperCol" :model="form">
      <a-form-model-item label="组名" prop="groupName">
        <a-input v-model="form.groupName" placeholder="请输入角色名称"/>
      </a-form-model-item>
      <a-form-model-item label="组长">
        <a-select placeholder="请选择组长" :default-value="form.doctorCode" @change="selectChange">
          <a-select-option v-for="d in doctorData" :value="d.value">
            {{d.label}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="下属医生">
        <a-select placeholder="请选择医生"  mode="multiple" :default-value="defaultData" @change="doctorIdChange">
          <a-select-option v-for="d in doctorId" :key="d.value">
            {{d.label}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
    <div class="footer">
      <a-row>
        <c-btn offset="18" span="3" type="primary" title="保存" @click="save"></c-btn>
        <c-btn span="3" type="default" title="取消" @click="$emit('update.visable',flase)"></c-btn>
      </a-row>
    </div>
  </main>
</template>

<script lang="js">
  import {add,update} from '@/api/doctorGroup';
  import {getDoctorList} from '@/api/common';
export default {
  props:{
    type:{
      default:'add'
    },
    editData:{
      default:{}
    },
  },
  data() {
    return {
      labelCol: {span: 4},
      wrapperCol: {span: 20},
      depTid:'',
      form: {
        groupName: "",
        userId:"",
        doctorCode:'',
        doctorName:'',
        doctorList:[],
      },
      defaultData:[],
      rules: {
        doctorName: [
          { required: true, message: '请输入医生名称', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入医生年龄', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择医生性别', trigger: 'blur' },
        ],
        tel: [
          { required: true, message: '请输入医生电话', trigger: 'blur' },
        ],
      },
      treeData : [],
      expandedKeys: [],
      autoExpandParent: true,
      checkedKeys: [],
      doctorData:[],
      doctorId:[],
    }
  },
  created() {
    console.log(this.type ,this.editData);
    this.form.userId=JSON.parse(localStorage.user).userId
    if(this.type ==='edit'){
      this.getEditData()
    }
    this.getDoctorList()
  },
  methods:{
    selectChange(e,option){
           this.form.doctorCode=e
           this.form.doctorName=option.componentOptions.children[0].text
    },
    doctorIdChange(e){
      console.log(e)
      this.form.doctorList=e.map((item)=>{
        return {dcTid:item}
      })
    },
    async getDoctorList(){
      const params = {
        doctorCode:null,
        doctorName:'',
        pageNum:1,
        pageSize:10000
      }
        const res = await getDoctorList(params);
        if (res['data']) {
          console.log(res.data)
          this.doctorData=res.data.map((item)=>{
            return {label:item.doctorName,value: item.doctorCode}
          })
          this.doctorId=res.data.map((item)=>{
            return {label:item.doctorName,value: item.dcTid}
          })
        }
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys);
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onCheck(checkedKeys) {
      console.log('onCheck', checkedKeys);
      this.checkedKeys = checkedKeys;
    },
    save(){
      this.$refs['roleForm'].validate((valid)=>{
        console.log(valid)
        if(valid){
          if(this.type==='add') this.setAddData();
          if(this.type ==='edit') this.setEditData()
        }else{
          this.$message.error('请完善表单信息')
        }
      })
    },
    async setAddData(){
      const params = {
        addpid:this.form.userId,
        doctorList:this.form.doctorList,
        groupname:this.form.groupName,
        opcode:this.form.doctorCode,
        opname:this.form.doctorName
      }
      console.log(this.form)
      const res = await add(params);
      if(res['msg'] ==='ok'){
        this.$message.success('添加成功')
        this.$emit('update:visible',false)
        this.$emit('success',res)
      }else{
        this.$message.error(res.msg)
      }
    },
    async setEditData(){
      console.log(this.editData)
      const params = {
        addpid:this.form.userId,
        doctorList:this.form.doctorList,
        groupname:this.form.groupName,
        dgId:this.form.dgId,
        opcode:this.form.doctorCode,
        opname:this.form.doctorName
      }
      console.log(this.form)
      const res = await update(params);
      if(res['msg'] ==='ok'){
        this.$message.success('编辑成功')
        this.$emit('update:visible',false)
        this.$emit('success',res)
      }else{
        this.$message.error(res.msg)
      }
    },
    getEditData(){
      this.form.addpid = this.editData.addpid;
      this.form.groupName = this.editData.groupname;
      this.form.doctorCode = this.editData.opcode;
      this.form.doctorName = this.editData.opname;
      this.form.dgId=this.editData.dgId;
      console.log(this.editData.doctorList)
      this.defaultData=this.editData.doctorList.map((item)=>{
        return item.dcTid
      })
    }
  }
}
</script>

<style scoped lang="stylus">

</style>
